﻿using RpcClient.Interface;
using RpcHelper;
using RpcSync.Service.Interface;

namespace RpcSync.Service.Service
{
    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class NodeRefreshService : INodeRefreshService
    {
        private IDictateNodeConfig _Config;
        private IUnityCollect _Unity;
        private Timer _Refresh;
        public NodeRefreshService(IDictateNodeConfig config, IUnityCollect unity)
        {
            _Unity = unity;
            _Config = config;
        }
        private int _ServerVer = -1;
        private int _DictateVer = -1;
        private void _RefreshServerNode(object state)
        {
            if (this._Config.DictateNodeVer == _DictateVer && _ServerVer == this._Config.ServerNodeVer)
            {
                return;
            }
            INodeLoadService node = this._Unity.Resolve<INodeLoadService>();
            if (this._Config.ServerNodeVer != this._ServerVer)
            {
                this._ServerVer = this._Config.ServerNodeVer;
                node.LoadServerNode();
            }
            if (this._Config.DictateNodeVer != this._DictateVer)
            {
                this._DictateVer = this._Config.DictateNodeVer;
                node.LoadDictateNode();
            }
        }
        public void Init()
        {
            if (_Refresh == null)
            {
                _Refresh = new Timer(new TimerCallback(this._RefreshServerNode), null, 100, Tools.GetRandom(60, 120) * 1000);
            }
        }
    }
}
